<template>
  <div class="home">
    <div class="chart" ref="chart"></div>
  </div>
</template>

<script setup>
import * as echarts from 'echarts';
import { ref, onMounted } from 'vue';

const chart = ref(null);
let myEchart = null;

const draw = () => {
  myEchart = echarts.init(chart.value);
  const option = {
    title: {
      text: '数据统计'
    },
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['订单', '销售额', '注册人数', '管理员人数'] // 修改后的图例
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    toolbox: {
      feature: {
        saveAsImage: {}
      }
    },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: ['第一周', '第二周', '第三周', '第四周', '第五周', '第六周', '第七周']
    },
    yAxis: {
      type: 'value',
      axisLabel: {
        // 自定义刻度标签
        formatter: function(value) {
          const labels = [300, 600, 900, 1200, 1500];
          const newLabels = [3000, 6000, 9000, 12000, 13000];
          const index = labels.indexOf(value);
          return index !== -1 ? newLabels[index] : value;
        }
      }
    },
    series: [
      {
        name: '订单',
        type: 'line',
        stack: 'Total',
        data: [120, 132, 101, 134, 90, 230, 210]
      },
      {
        name: '销售额',
        type: 'line',
        stack: 'Total',
        data: [220, 182, 191, 234, 290, 330, 310]
      },
      {
        name: '注册人数',
        type: 'line',
        stack: 'Total',
        data: [150, 232, 201, 154, 190, 330, 410]
      },
      {
        name: '管理员人数',
        type: 'line',
        stack: 'Total',
        data: [320, 332, 301, 334, 390, 330, 320]
      }
    ]
  };
  myEchart.setOption(option);
};

onMounted(() => {
  draw();
});

window.addEventListener('resize', () => {
  if (myEchart) {
    myEchart.resize();
  }
});
</script>

<style scoped lang="scss">
.home {
  width: 100%;
  height: 100%;
}

.chart {
  width: 100%;
  height: 80%;
}
</style>